﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArtemisTest
{

    #region Entity System
    //New components must be added to list

    public class EntitySystem
    {
        //ComponentFactory stores all the components that are created.
        ComponentFactory _componentFactory;
        public ComponentFactory Components { get { return _componentFactory; } }

        //bool is for whether the entity is alive or not.
        List<Tuple<long,bool>> _hidden;   //All entitys that dont have a position
        QT<bool> _entitys;    //Spatially Indexed

        public List<Tuple<bool,long>> Indexed2dEntitys { get { return _entitys.GetAllData(); } }

        public EntitySystem()
        {
            _componentFactory = new ComponentFactory();
            _hidden = new List<Tuple<long, bool>>();
            _entitys = new QT<bool>(new AABB(0, 0, 800, 600));
        }
        
        //Reconile with EntityFactory - Where should the event listening delgation be handled?
        //Allows the retreival of entitys
        public void AddEntity(long uid)
        {
            Position pos = (Position)_componentFactory.GetComponent(ComponentType.position,uid);
            xy p = new xy(pos.X, pos.Y);

            if (pos != null)
            {
                _entitys.Insert(p, true, uid);
            }
            else
            {
                _hidden.Add(new Tuple<long,bool>(uid,true));
            }

        }

        public List<QT<bool>.QTData> GetEntitysInBounds(AABB bounds,long uidPerspective = -1)
        {
            return _entitys.GetPointsInRange(bounds);
        }

        //Rethink this - Make this implement abstract System class
        public void UpdateIndexedPosition(long uid)
        {
            Position pos = (Position)_componentFactory.GetComponent(ComponentType.position, uid);
            xy p = new xy(pos.X, pos.Y);
            Console.WriteLine("Updated Entity Pos Index");
            _entitys.UpdatePosition(p, uid);
        }
    }


}
    #endregion